package com.hebeu.device.controller;

import com.hebeu.device.pojo.DService;
import com.hebeu.device.pojo.DServiceDevice;
import com.hebeu.device.pojo.view.ViewService;
import com.hebeu.device.service.NotifyRepairService;
import com.hebeu.device.service.ServiceDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 报修管理控制器
 * @Author 言立慧
 * @Date 2019/6/16 19:21
 */
@RestController
@RequestMapping("/repair")
public class NotifyRepairController {
    private NotifyRepairService notifyRepairService;
    private ServiceDeviceService serviceDeviceService;

    @Autowired
    public NotifyRepairController(NotifyRepairService notifyRepairService, ServiceDeviceService serviceDeviceService) {
        this.notifyRepairService = notifyRepairService;
        this.serviceDeviceService = serviceDeviceService;
    }

    /**
     * 查询所有维修单
     *
     * @return
     */
    @GetMapping("/repairList")
    public List<DService> getRepairList() {
        List<DService> dServices = notifyRepairService.selectAllDServices();
        return dServices;
    }

    /**
     * 新增报修记录
     *
     * @param viewService
     */
    @PostMapping("/addService")
    public Integer addService(ViewService viewService) {
        // 生成报修记录
        Integer sId = notifyRepairService.selectAllDServices().size() + 1;
        DService service = notifyRepairService.setService(sId, viewService);
        // 生成设备记录
        DServiceDevice serviceDevice = serviceDeviceService.setDevice(sId, viewService);

        // 写入数据库
        serviceDeviceService.addServiceDevice(serviceDevice);
        notifyRepairService.addService(service);
        // 返回id，用于下一次的查询
        return sId;
    }

    /**
     * 提交客户确认单
     *
     * @param serviceId 服务id
     */
    @PostMapping("/clientSure")
    public void clientSure(Integer serviceId) {
        DService dService = new DService();
        dService.setsId(serviceId);
        // 将服务状态id设置为等待分配
        dService.setsStatus(1);
        notifyRepairService.updateService(dService);
    }

    /**
     * 修改报修记录
     *
     * @param viewService
     * @return
     */
    @PostMapping("/alterService")
    public Integer alterService(ViewService viewService) {
        // 生成报修记录
        DService service = notifyRepairService.setService(viewService.getsId(), viewService);
        // 生成设备记录
        DServiceDevice dServiceDevice = serviceDeviceService.setDevice(viewService.getsId(), viewService);
        // 写入数据库更新
        serviceDeviceService.updateServiceDevice(dServiceDevice);
        notifyRepairService.updateService(service);
        // 返回id，用于下一次查询
        return viewService.getsId();
    }

    /**
     * 查询服务信息
     *
     * @param serviceId
     * @return
     */
    @PostMapping("/getServiceInfo")
    public Object getServiceInfo(Integer serviceId) {
        // 创建map，用于返回多种数据
        Map<String, Object> infoMap = new HashMap<>();
        DServiceDevice serviceDevice = serviceDeviceService.getServiceDeviceByServiceId(serviceId);
        DService service = notifyRepairService.getServiceById(serviceId);
        infoMap.put("device", serviceDevice);
        infoMap.put("service", service);
        return infoMap;
    }
}
